import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_page/home_business/search_page/search_main_logic.dart';
import 'package:tiktokadult/hj_page/home_business/search_page/widget/search_app_bar.dart';
import 'package:tiktokadult/hj_page/home_business/search_page/widget/search_histoty_view.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/banner/ads_grid_view_widget.dart';

import '../../../hj_model/video_model.dart';

class SearchMainPage extends StatelessWidget {
  SearchMainPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<VideoSearchMainController>(
      init: VideoSearchMainController(),
      builder: (logic) {
        return Scaffold(
          resizeToAvoidBottomInset: false,
          body: Column(
            children: [
              SearchAppBar(
                onSubmitted: (value) => logic.onSubmitted(value),
              ),
              Expanded(
                child: NestedScrollView(
                  headerSliverBuilder: (context, innerBoxIsScrolled) {
                    return [
                      SliverToBoxAdapter(
                        child: AdsGridViewWidget(
                          32,
                          isBannerStyle: false,
                          isBigStyle: false,
                          padding: EdgeInsets.symmetric(vertical: 12.h, horizontal: 12.w),
                        ),
                      ),
                      SliverToBoxAdapter(
                        child: SearchHistoryView(
                          onHistoryClick: (keyword) => logic.onSubmitted(keyword),
                        ),
                      ),
                    ];
                  },
                  body: CustomScrollView(
                    slivers: <Widget>[
                      12.sliverSizeBoxH,
                      SliverToBoxAdapter(
                        child: Text(
                          '热门搜索',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 14,
                            fontWeight: FontWeight.w500,
                          ),
                        ).paddingSymmetric(horizontal: 12),
                      ),
                      12.sliverSizeBoxH,
                      SliverPadding(
                        padding: EdgeInsets.symmetric(horizontal: 12),
                        sliver: DecoratedSliver(
                          decoration: BoxDecoration(
                            color: Colors.white.withOpacity(0.1),
                            borderRadius: BorderRadius.circular(3),
                          ),
                          sliver: SliverList.builder(
                            itemCount: logic.dataArr?.length ?? 0,
                            itemBuilder: (context, index) {
                              return _buildHotRankCell(index, logic.dataArr![index]);
                            },
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildHotRankCell(int index, VideoModel model) {
    return InkWell(
      onTap: () {},
      child: Container(
        padding: EdgeInsets.fromLTRB(12, 6, 12, 6),
        child: Row(
          children: [
            Container(
              width: 20,
              alignment: Alignment.center,
              child: () {
                if (index < 3)
                  return Image.asset("hot_${index + 1}.webp".homePath, width: 20);
                else
                  return Text(
                    "${index + 1}",
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                      color: Color(0xE5FFFFFF),
                    ),
                  );
              }(),
            ),
            4.sizeBoxW,
            Expanded(
              child: Text(
                "${model.title}",
                maxLines: 1,
                style: TextStyle(
                  fontSize: 12,
                  color: Color(0xE5FFFFFF),
                ),
              ),
            ),
            6.sizeBoxW,
            Text(
              "${model.playCount?.humanizeFormat ?? ""}",
              style: TextStyle(
                fontSize: 12,
                color: Color(0xE5FFFFFF),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
